﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using YEngineLib;
using YEngineLib.Animation;

//using YEngineLib;

namespace Spyfall.Objects
{

    class Obstacles : Object
    {
        public Texture2D tex;
        //List<Animation> obstacleAnimationList;
        //Animation idleAnimation;

        //public Vector2 origin;
        public Vector2 position;
        public Vector2 velocity;
        public Vector2 origin;
        public float rotation;

        public Obstacles()
        {
            ObjectID = Object_Type.OBSTACLES;

            //// Animation Stuff Start ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            //texture = YEngine.Game.Content.Load<Texture2D>("Graphics/obstacle01");
            //obstacleAnimationList = new List<Animation>();
            //idleAnimation = new Animation();
            //idleAnimation.Initialize(texture, position, 94, 60, 2, 0.1f, Color.White, 1f, true);
            //obstacleAnimationList.Add(idleAnimation);
            //// Animation Stuff End   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        }

        public void Initialize(Texture2D texture, float positionX, float positionY, float velocityX, float velocityY)
        {
            tex = texture;
            position.X = positionX;
            position.Y = positionY;
            velocity.X = velocityX;
            velocity.Y = velocityY;

            ColRect = new Rectangle((int)position.X, (int)position.Y, this.tex.Width, this.tex.Height);
            origin.X = this.tex.Width / 2;
            origin.Y = this.tex.Height / 2;

            rotation = 0.0f;
        }

        public override void Update(float deltaTime)
        {

            if (this.ObstacleID == Obstacle_Type.PROP)
            {
                origin.X = 58;
                origin.Y = 67;
            }
            //// Animation Stuff Start ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            //for (int i = obstacleAnimationList.Count - 1; i >= 0; i--)
            //{
            //    obstacleAnimationList[i].Update(deltaTime);
            //    if (obstacleAnimationList[i].Active == false)
            //    {
            //        obstacleAnimationList.RemoveAt(i);
            //    }
            //}
            //// Animation Stuff End   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

            //MOVEMENT
            position.Y += (velocity.Y * deltaTime);

            if (this.ObstacleID == Obstacle_Type.CRATE || this.ObstacleID == Obstacle_Type.PROP)
            {
                ColRect = new Rectangle((int)position.X - this.tex.Width / 2, (int)position.Y - this.tex.Height / 2, this.tex.Width, this.tex.Height);                
            }
            else
                ColRect = new Rectangle((int)position.X, (int)position.Y, this.tex.Width, this.tex.Height);

            //ColRect = new Rectangle(int.Parse(position.X.ToString()), int.Parse(position.Y.ToString()), idleTexture.Bounds.Width / 2, idleTexture.Height / 2);

            if (this.ObstacleID == Obstacle_Type.CRATE)
	        {
                rotation -= deltaTime * 1.5f;
	        }

            if (this.ObstacleID == Obstacle_Type.PROP)
            {
                rotation += deltaTime * 55;
            }
                
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            //// Animation Stuff Start ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            //for (int i = 0; i < obstacleAnimationList.Count; i++)
            //{
            //    obstacleAnimationList[i].Draw(spriteBatch);
            //}
            //// Animation Stuff End   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


            if (this.ObstacleID == Obstacle_Type.CRATE || this.ObstacleID == Obstacle_Type.PROP)
            {
              YEngine.Graphics.SpriteBatch.Draw(tex, position, null, Color.White, rotation, origin, 1.0f, 0, 0.0f);
            }
            else
            {
                YEngine.Graphics.SpriteBatch.Draw(tex, position, Color.White);
            }


            // draw collision rects
            Texture2D dummyTexture;
            Rectangle dummyRectangle;
            dummyRectangle = this.ColRect;
            dummyTexture = new Texture2D(YEngine.Game.GraphicsDevice, 1, 1);
            dummyTexture.SetData(new Color[] { Color.White });
            

            //YEngine.Graphics.SpriteBatch.Draw(dummyTexture, dummyRectangle, Color.White);

            //YEngine.Graphics.SpriteBatch.Draw(tex, position, null, Color.White, 360, 0, SpriteEffects.None, 0);
        }

        public override bool Collide(Object tar)
        {
            bool res = ColRect.Intersects(tar.ColRect);
            return res;
        } 
    }
}

